﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace zmMoniter
{
    public partial class frmNavArtifHistory : zmMoniter.Basefrm
    {
        private mainForm main = null;
        struct CmbInfo
        {
            public int _key; public string _text;
            public CmbInfo(int key, string text)
            {
                _key = key; _text = text;
            }
            public override string ToString()
            {
                return _text;
            }
        }
        private string sSQL = "";
        public frmNavArtifHistory(mainForm frm)
        {
            InitializeComponent();
            main = frm;
            sSQL = "";
        }
        private void selectDam(string damName)
        {
            cmbox_damBlock.Properties.Items.Clear();
            cmbox_damBlock.Text = (string)cmbox_damBlock.Tag;

            if (damName == "上坝块")
            {
                for (int i = 1; i < 20; i++)
                {
                    cmbox_damBlock.Properties.Items.Add(new CmbInfo(i, "坝段" + i.ToString()));
                }
            }
            else if (damName == "下坝块")
            {
                for (int i = 2; i < 19; i++)
                {
                    cmbox_damBlock.Properties.Items.Add(new CmbInfo(i, "坝段" + i.ToString()));
                }
            }
        }
        private void cmbox_dam_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectDam(cmbox_dam.EditValue.ToString());

        }
        private void selectDamBlock(string sDam, string sDamBlock)
        {
            cmbox_deck.Properties.Items.Clear();
            cmbox_deck.Text = (string)cmbox_deck.Tag;

            PublicDataCls db = new PublicDataCls();
            db.OpenConn();
            string sSQL = "";
            if (sDam == "上坝块")
                sSQL = "SELECT * FROM 仓面信息 WHERE 上下游块='S' AND 坝段=" + sDamBlock;
            else if (sDam == "下坝块")
                sSQL = "SELECT * FROM 仓面信息 WHERE 上下游块='X' AND 坝段=" + sDamBlock;
            DataTable dt = db.ReturnDataTable(sSQL);
            db.CloseConn();
            if (db == null)//modify by lance 2013-1-20
            {
                return;
            }
            int iCount = dt.Rows.Count;
            for (int i = 0; i < iCount; i++)
            {
                DataRow dataRow = dt.Rows[i];
                int iIDSQL = int.Parse(dataRow["编号"].ToString().Trim());
                string sNameSQL = dataRow["仓面名称"].ToString().Trim();
                //string sMinHeight = dataRow["最低高程"].ToString().Trim();
                //string sMaxHeight = dataRow["最高高程"].ToString().Trim();
                // string sVibRange = dataRow["振捣边界点"].ToString().Trim();
                // string sDeckRange = dataRow["仓面控制点"].ToString().Trim();
                cmbox_deck.Properties.Items.Add(new CmbInfo(iIDSQL, sNameSQL.ToString()));

            }
        }
        private void cmbox_damBlock_SelectedIndexChanged(object sender, EventArgs e)
        {
            CmbInfo p = new CmbInfo();
            p = (CmbInfo)(cmbox_damBlock.SelectedItem);
            int value = p._key;
            string damName = cmbox_dam.EditValue.ToString();
            selectDamBlock(damName, value.ToString());
        }
      
       
       
        private void pictureBox_Btn_Click(object sender, EventArgs e)
        {
            label_msg.Text = "正在查找中...";
            label_msg.Visible = true;
            if (cmbox_deck.Text != "请选择仓面")
            {
                CmbInfo p = (CmbInfo)(cmbox_deck.SelectedItem);
                int iDeckID = p._key;

                sSQL = "select 人工振捣记录表.编号 AS 人工编号," +
                    "仓面信息.仓面名称 AS 仓面名称," +
                    "to_char(人工振捣记录表.进仓时间,'yyyy-mm-dd hh24:mi:ss') AS 进仓时间,to_char(人工振捣记录表.出仓时间,'yyyy-mm-dd hh24:mi:ss') AS 出仓时间," +
                    "人工振捣基本信息表.姓名,人工振捣基本信息表.负责部位,人工振捣基本信息表.RFID " +
                    "FROM 人工振捣记录表 LEFT OUTER JOIN " +
                    "仓面信息 ON 人工振捣记录表.仓面号 = 仓面信息.编号 " +
                    "LEFT OUTER JOIN 人工振捣基本信息表 ON 人工振捣记录表.人工编号 = 人工振捣基本信息表.编号 " +
                    "WHERE 人工振捣记录表.仓面号=" + iDeckID;
                if (dateEdt_Start.Text != "")
                {
                    sSQL += " AND 进仓时间>=to_date('" + dateEdt_Start.Text + "','YYYY/MM/DD')";
                }
                if (dateEdt_End.Text != "")
                {
                    sSQL += " AND 出仓时间<=to_date('" + dateEdt_End.Text + "','YYYY/MM/DD')";
                }
                if (textEdt_ID.Text != "")
                {
                    sSQL += " AND 人工振捣基本信息表.RFID='" + textEdt_ID.Text + "'";
                }
                if (textEdt_Name.Text != "")
                {
                    sSQL += " AND 人工振捣基本信息表.姓名='" + textEdt_Name.Text + "'";
                }
                sSQL += " Order by 进仓时间 DESC";
            }
            else if (cmbox_damBlock.Text != "请选择坝段")
            {
                CmbInfo p = (CmbInfo)(cmbox_damBlock.SelectedItem);
                int iDamBlockID = p._key;
                string damName = cmbox_dam.EditValue.ToString();
                if (damName == "上坝块")
                {
                    damName = "S";
                }
                else if (damName == "下坝块")
                {
                    damName = "X";
                }

                sSQL = "select 人工振捣记录表.编号 AS 人工编号," +
                   "仓面信息.仓面名称 AS 仓面名称," +
                   "to_char(人工振捣记录表.进仓时间,'yyyy-mm-dd hh24:mi:ss') AS 进仓时间,to_char(人工振捣记录表.出仓时间,'yyyy-mm-dd hh24:mi:ss') AS 出仓时间," +
                   "人工振捣基本信息表.姓名,人工振捣基本信息表.负责部位,人工振捣基本信息表.RFID " +
                   "FROM 人工振捣记录表 LEFT OUTER JOIN " +
                   "仓面信息 ON 人工振捣记录表.仓面号 = 仓面信息.编号 " +
                   "LEFT OUTER JOIN 人工振捣基本信息表 ON 人工振捣记录表.人工编号 = 人工振捣基本信息表.编号 " +
                   "WHERE 仓面信息.坝段=" + iDamBlockID + " AND 仓面信息.上下游块='" + damName + "'";
                if (dateEdt_Start.Text != "")
                {
                    sSQL += " AND 进仓时间>=to_date('" + dateEdt_Start.Text + "','YYYY/MM/DD')";
                }
                if (dateEdt_End.Text != "")
                {
                    sSQL += " AND 出仓时间<=to_date('" + dateEdt_End.Text + "','YYYY/MM/DD')";
                }
                if (textEdt_ID.Text != "")
                {
                    sSQL += " AND 人工振捣基本信息表.RFID='" + textEdt_ID.Text + "'";
                }
                if (textEdt_Name.Text != "")
                {
                    sSQL += " AND 人工振捣基本信息表.姓名='" + textEdt_Name.Text + "'";
                }
                sSQL += " Order by 进仓时间 DESC";
            }
            else if (cmbox_dam.Text != "请选择坝块")
            {

                string damName = cmbox_dam.EditValue.ToString();
                if (damName == "上坝块")
                {
                    damName = "S";
                }
                else if (damName == "下坝块")
                {
                    damName = "X";
                }
                sSQL = "select 人工振捣记录表.编号 AS 人工编号," +
                   "仓面信息.仓面名称 AS 仓面名称," +
                   "to_char(人工振捣记录表.进仓时间,'yyyy-mm-dd hh24:mi:ss') AS 进仓时间,to_char(人工振捣记录表.出仓时间,'yyyy-mm-dd hh24:mi:ss') AS 出仓时间," +
                   "人工振捣基本信息表.姓名,人工振捣基本信息表.负责部位,人工振捣基本信息表.RFID " +
                   "FROM 人工振捣记录表 LEFT OUTER JOIN " +
                   "仓面信息 ON 人工振捣记录表.仓面号 = 仓面信息.编号 " +
                   "LEFT OUTER JOIN 人工振捣基本信息表 ON 人工振捣记录表.人工编号 = 人工振捣基本信息表.编号 " +
                   "WHERE 仓面信息.上下游块='" + damName + "'";
                if (dateEdt_Start.Text != "")
                {
                    sSQL += " AND 进仓时间>=to_date('" + dateEdt_Start.Text + "','YYYY/MM/DD')";
                }
                if (dateEdt_End.Text != "")
                {
                    sSQL += " AND 出仓时间<=to_date('" + dateEdt_End.Text + "','YYYY/MM/DD')";
                }
                if (textEdt_ID.Text != "")
                {
                    sSQL += " AND 人工振捣基本信息表.RFID='" + textEdt_ID.Text + "'";
                }
                if (textEdt_Name.Text != "")
                {
                    sSQL += " AND 人工振捣基本信息表.姓名='" + textEdt_Name.Text + "'";
                }
                sSQL += " Order by 进仓时间 DESC";
            }
            else
            {
                sSQL = "select 人工振捣记录表.编号 AS 人工编号," +
                   "仓面信息.仓面名称 AS 仓面名称," +
                   "to_char(人工振捣记录表.进仓时间,'yyyy-mm-dd hh24:mi:ss') AS 进仓时间,to_char(人工振捣记录表.出仓时间,'yyyy-mm-dd hh24:mi:ss') AS 出仓时间," +
                   "人工振捣基本信息表.姓名,人工振捣基本信息表.负责部位,人工振捣基本信息表.RFID " +
                   "FROM 人工振捣记录表 LEFT OUTER JOIN " +
                   "仓面信息 ON 人工振捣记录表.仓面号 = 仓面信息.编号 " +
                   "LEFT OUTER JOIN 人工振捣基本信息表 ON 人工振捣记录表.人工编号 = 人工振捣基本信息表.编号";
                bool bflag = false;
                if (dateEdt_Start.Text != "" && dateEdt_End.Text != "")
                {
                    sSQL += " WHERE 进仓时间>=to_date('" + dateEdt_Start.Text + "','YYYY/MM/DD')" +
                        " AND 进仓时间<=to_date('" + dateEdt_End.Text + "','YYYY/MM/DD')";
                    bflag = true;
                }
                else if (dateEdt_Start.Text == "" && dateEdt_End.Text != "")
                {
                    sSQL += " WHERE 进仓时间<=to_date('" + dateEdt_End.Text + "','YYYY/MM/DD')";
                    bflag = true;
                }
                else if (dateEdt_Start.Text != "" && dateEdt_End.Text == "")
                {
                    sSQL += " WHERE 进仓时间>=to_date('" + dateEdt_Start.Text + "','YYYY/MM/DD')";
                    bflag = true;
                }

                if (textEdt_ID.Text != "")
                {
                    if (bflag == true)
                        sSQL += " AND 人工振捣基本信息表.RFID='" + textEdt_ID.Text + "'";
                    else
                        sSQL += " WHERE 人工振捣基本信息表.RFID='" + textEdt_ID.Text + "'";
                    bflag = true;
                }
                if (textEdt_Name.Text != "")
                {
                    if (bflag == true)
                        sSQL += " AND 人工振捣基本信息表.姓名='" + textEdt_Name.Text + "'";
                     else
                        sSQL += " WHERE 人工振捣基本信息表.姓名='" + textEdt_Name.Text + "'";
                }
                sSQL += " Order by 进仓时间 DESC";
            }
            if (sSQL == "" || main.frHistory == null)
            {
                label_msg.Text = "结果：查找出错！";
                label_msg.Visible = true;
                return;
            }
            sSQL = "select rownum AS 编号,t.* from(" + sSQL + ") t";
            PublicDataCls db = new PublicDataCls();
            db.OpenConn();
            DataTable dt = db.ReturnDataTable(sSQL);
            if (dt == null)//modify by lance 2013-1-20
            {
                label_msg.Text = "连接数据库失败!";
                return;
            }
            int iCount = dt.Rows.Count;
            label_msg.Text = "结果：共查找到 " + iCount + " 条记录";
            label_msg.Visible = true;
            db.CloseConn();
            main.frHistory.gridCtl_ArtfHistory.DataSource = dt;
            main.frmManger("Artificial");

            if (main.barBtn_DrawDeckPlg.Enabled == true)//在报警历史记录浏览的时候，禁用仓面编辑工具
                main.barBtn_DrawDeckPlg.Enabled = false;
        }
    }
}
